<?php
namespace plugins\feedback\controller;

use app\common\lib\model_operate\Transit; //引入此类
use cmf\controller\PluginAdminBaseController;
use think\Db;
use think\Request;

/**
 * 
 */
class AdminIndexController extends PluginAdminBaseController
{
    private  $pageNum = "20";



    /**
     * 反馈首页
     */
    public function index()
    {
        $data = $this->request->param();
        $where = 1;
        if( !empty($data['status']) ) $where .= ' and status ='.$data [ 'status' ]; 
        if( !empty($data['type']) ) $where .= " and type like '%".$data['type']."%'"; 
        if( !empty($data['mobile']) ) $where .= " and mobile like '%".$data['mobile']."%'"; 

        $sear['pagesize'] = isset( $data['pagesize'] ) ? $data [ 'pagesize' ] : $this->pageNum;
        $sear['type'] = isset( $data['type'] ) ? $data [ 'type' ] : '';
        $sear['mobile'] = isset( $data['mobile'] ) ? $data [ 'mobile' ] : '';
        $sear['status'] = isset( $data['status'] ) ? $data [ 'status' ] : '';

        $list = db('feedback')->where($where)->paginates($sear['pagesize'] , false ,['query'=>$sear]);

        $list->appends($data);
        $this->assign('list', $list);
        $this->assign('sear',$sear);
        $this->assign('type', db('feedback_type')->select());
        $this->assign('page', $list->render());

        return $this->fetch();

    }

    

    /**
     * 查看-反馈意见
     */
    public function viewPage()
    {
        $param = $this->request->param();

        $data=['id'=>$param['id']];

        $data = db('feedback')->where($data)->find();

        $img = json_decode($data['img'],true);

        $this->assign('data',$data);

        $this->assign('img',$img);

        return $this->fetch('viewPage');
    }

    /**
     * 标记已读/未读
     */
    public function readUnread()
    {
        $param = $this->request->param();
        $ids = $param['id'];
        $data = db('feedback')->where('id','in',$ids)->select();
        $status = [];
        foreach ( $data as $key => $value )
        {
            $status[]=$value['status'];
        
            $data[$key]=$value;
        }
        $array = $this->arrayValEqu($status);
        if( $array == false) return api_json_echo(false,'批量选择的状态不一致，请选统一状态的进行操作','',100);
        $val = $array == 1 ? 2 : 1 ;
        $res = db('feedback')->where('id','in',$ids)->update(['status'=>$val]);
        if($res) return api_json_echo(true,'成功','',200);
        return api_json_echo(false,'失败','',101);
    }

    /**
     * 验证数组值是否相同
     */
    public function arrayValEqu($array)
    {
        $tmp = 0;
        foreach( $array as  $k => $v )
        {
            if( $tmp == 0 ) $tmp=$v;
            if( $tmp != $v ) return false;
        }
        return $tmp;
    }


    /**
     * 删除反馈
     */
    public function delete()
    {
        $param = $this->request->param();
        $ids = $param['id'];
        $res = db('feedback')->where('id','in',$ids)->delete();
        if($res)return api_json_echo(true,'成功','',200);
        return api_json_echo(false,'失败','',100);
    }


    /**
     * 设置反馈类型
     */
    public function config()
    {
        $param = $this->request->param();

        $list = db('feedback_type')->select();
    
        $this->assign('list',$list);

        return $this->fetch('config');
    }

    /**
     * 添加-反馈类型
     */
    public function add()
    {
        $param = $this->request->post();

        $data=['title'=>$param['title']];

        $info = db('feedback_type')->insert($data);

        if($info) return api_json_echo(true,'成功','',200);

        return api_json_echo(false,'失败','',100);
    }



    /**
     * 删除-反馈类型
     */
    public function deleteType()
    {
        $param = $this->request->post();
        
        $data=['id'=>$param['id']];

        $info = db('feedback_type')->delete($data);

        if($info) return api_json_echo(true,'成功','',200);

        return api_json_echo(false,'失败','',100);
    }




}